package tongwentongshu.com.app.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.activity.PersonalHomePageActivity;
import tongwentongshu.com.app.activity.ReadingDetailsActivity;
import tongwentongshu.com.app.adapter.MyReadingAdapter;
import tongwentongshu.com.app.adapter.MyShareAdapter;
import tongwentongshu.com.app.adapter.PreShareAdapter;
import tongwentongshu.com.app.bean.MyReadingBean;
import tongwentongshu.com.app.bean.MyShareBean;
import tongwentongshu.com.app.bean.SumEvent;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;

/**
 * Created by XiongBin on 2017/7/12.
 */

public class PerShareFragment extends BasePagerFragment implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private Map<String, String> map;
    protected boolean onRefreshJudge = false;
    private PreShareAdapter myShareAdapter;

    public PerShareFragment(Activity activity) {
        super(activity);
    }

    @Override
    public void initData() {
        super.initData();
        initList();
        initMyReading();
    }

    private void initList() {
        onRefreshJudge = true;
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        List arr = new ArrayList<MyReadingBean.DataBean>();
        myShareAdapter = new PreShareAdapter(getActivity(), arr);
        myShareAdapter.setOnLoadMoreListener(this, mRecyclerView);
        myShareAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        mRecyclerView.setAdapter(myShareAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                Bundle b = new Bundle();
                b.putBoolean("pager_Judge", true);
                b.putString("pagerType", "1");
                b.putString("bookId", myshare.getData().get(position).getBookid());
                startActivity(ReadingDetailsActivity.class, b);
            }
        });
    }

    MyShareBean myshare;

    void initMyReading() {
        map = new ArrayMap<>();
        if (!TextUtils.isEmpty(userId))
            map.put("id", userId);
        if (!TextUtils.isEmpty(Cache.getToken()))
            map.put("token", Cache.getToken());
        map.put("pagesize", "50");

        UpdateFractory.getBuild()
                .name("MyShareCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<MyShareBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(MyShareBean myshare, String msg) {
                        PerShareFragment.this.myshare = myshare;
                        if (!TextUtils.isEmpty(myshare.getNum())) {
                            EventBus.getDefault().post(new SumEvent("", myshare.getNum()));
                        }
                        chengeData(myshare);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }

    @Override
    public void onRefresh() {
        onRefreshJudge = true;
//        pullToRefreshAdapter.setEnableLoadMore(false);
        initMyReading();
    }

    @Override
    public void onLoadMoreRequested() {
        myShareAdapter.loadMoreEnd(true);
    }

    void chengeData(MyShareBean myreading) {
        View empty = mActivity.getLayoutInflater().inflate(R.layout.empty_no_data, (ViewGroup) mRecyclerView.getParent(), false);
        //pullToRefreshAdapter.setPreLoadNumber(3);
        myShareAdapter.setEmptyView(empty);
        if (myreading.getData() != null && myreading.getData().size() > 0) {
            if (onRefreshJudge) {
                Log.e("onRefreshJudge", "onRefreshJudge: " + onRefreshJudge);
                myShareAdapter.setNewData(myreading.getData());
                mSwipeRefreshLayout.setRefreshing(false);
                myShareAdapter.setEnableLoadMore(true);
                onRefreshJudge = false;
            }
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            myShareAdapter.setEnableLoadMore(true);
            onRefreshJudge = false;
        }
    }
}